package com.idroid.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * 可以嵌套滚动的ScrollView
 * Created by zhangbo on 2016/10/20.
 */
public class NestedScrollView extends NoAutoScrollView{
    //嵌套滑动的子View
    View mNestedChildView;
    public void setNestedChildView(View v){
        mNestedChildView = v;
    }

    public NestedScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if(mNestedChildView!=null && inRangeOfView(mNestedChildView,ev)){  //当MotionEvent在需要嵌套滚动得到子View范围内时 取消ScrollView的事件拦截
            return false;
        }
        return super.onInterceptTouchEvent(ev);
    }

    /**
     * 判断MotionEvent是否在View范围中
     * @return
     */
    private boolean inRangeOfView(View view, MotionEvent ev){
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];

        if(ev.getRawX() < x || ev.getRawX() > (x + view.getWidth()) || ev.getRawY() < y || ev.getRawY() > (y + view.getHeight())){
            return false;
        }
        return true;
    }
}
